/*
 * Copyright 2010 WANdisco, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.googlecode.svnj;

import java.io.File;
import java.io.IOException;

import org.tigris.subversion.javahl.SVNClient;
import org.junit.Before;
import org.tigris.subversion.javahl.SVNClientInterface;

import static org.junit.Assert.*;

/** base class for our unit tests with nifty helpers */
public class SVNJTestBase {
    
    /* runtime directory where tests should create artifacts */
    protected static File testDir;
    
    @Before
    public void setUp() throws Exception {
        assertTestDir();
    }

    protected static void assertTestDir() throws IOException { 
        String path = System.getProperty("test.dir");
        if (path == null) {
            fail("system prop 'test.dir' undefined");
        }
        testDir = new File(path).getCanonicalFile();
        
    }
    
    /* helper to create the test repo */
    protected static File createTestRepo() throws Exception {
        return createTestRepo("alphabet");
    }
    /* helper to create a given test repo */
    protected static File createTestRepo(String repoName) throws Exception {
        assertTestDir();
        File dumpFile = new File("test/data/" + repoName + ".dump");
        if (!dumpFile.isFile()) {
            throw new IllegalArgumentException(
                    "did not find expected dump file: "
                    + dumpFile.getAbsolutePath()
                    );
        }
        File repoDir = new File(testDir, "repo-" + repoName);
        if (!repoDir.isDirectory()) {
            // assume PWD is root of svnj project
            TestUtils.createRepo(repoDir, dumpFile);
            System.out.println("[SVNJTestBase] created repo at " + repoDir.getAbsolutePath());
        }
        return repoDir;
    }
    
    protected static SVNClientInterface createTestClient() throws Exception {
        SVNClient c = new SVNClient();
        c.setConfigDirectory(testDir.getAbsolutePath());
        return c;
    }

    protected void p(String msg) {
        String cname = getClass().getSimpleName();
        System.err.println("[" + cname + "] " + msg);
    }
}
